

#include "goldcoinscene.h"
#include "resourcemanager.h"
#include "log.h"
#include "messagequeue.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "cocos-ext.h"

static int arrCoinPoints[] = {4000,12500,30000,50000,100000,350000};
static const char* arrCoins[] = {"4,000","12,500","30,000","50,000","100,000","350,000"};

GoldCoinScene::GoldCoinScene():
  _layer(nullptr)
{
}

GoldCoinScene::~GoldCoinScene() {
}

bool GoldCoinScene::init() {
	if (!CCScene::init()) {
		return false;
	}

	CCDirector::sharedDirector()->getKeypadDispatcher()->addDelegate(this);

	_layer = CCLayer::create();
	addChild(_layer);

	CCSprite *spr = CCSprite::create("textures/ui_bg.png");
	spr->setAnchorPoint(CCPointZero);

	CCSize size = spr->getContentSize();
	spr->setScaleX(GM->screenSize.width / size.width);
	spr->setScaleY(GM->screenSize.height / size.height);
	_layer->addChild(spr);

	auto btnExit = RM->makeBackButton(this, menu_selector(GoldCoinScene::onClose), _layer, true);
	btnExit->setPosition(XY_SCALE(50, 524));

	CCSprite *sprMoneyPad = MAKE_SPRITE("money_pad.png");
	sprMoneyPad->setPosition(XY_SCALE(GameManager::BASE_WIDTH, GameManager::BASE_HEIGHT - 30));
	sprMoneyPad->setAnchorPoint(ccp(1.0f, 0.5f));
	_layer->addChild(sprMoneyPad);

	sprintf(buf, "%d", GDM->money);
	_labelMoney = CCLabelTTF::create(buf, "", 24);
	_labelMoney->setPosition(ccp(76, 30));
	_labelMoney->setColor(ccc3(237, 199, 143));
	_labelMoney->setAnchorPoint(ccp(0.5f, 0.5f));
	sprMoneyPad->addChild(_labelMoney);

	CCPoint arrPos[] = {
			ccp(265,435), ccp(485,435), ccp(705,435),
			ccp(265,175), ccp(485,175), ccp(705,175),};

	static const char* arrIcons[] = {
			"gold_pack_1.png","gold_pack_1.png","gold_pack_1.png","gold_pack_1.png","gold_pack_1.png","gold_pack_1.png",
	};

	for(int i = 0; i < sizeof(arrPos) / sizeof(CCPoint); i++) {
		CCSprite * sprCoinPad = MAKE_SPRITE(arrIcons[i]);
		sprCoinPad->setPosition(SCALE_PT(arrPos[i]));
		sprCoinPad->setAnchorPoint(ccp(0.5f, 0.5f));
		_layer->addChild(sprCoinPad);

		CCLabelTTF* labelCoins = CCLabelTTF::create(arrCoins[i], "", 16);
		labelCoins->setPosition(ccp(85, 22));
		labelCoins->setColor(ccc3(237, 199, 143));
		labelCoins->setAnchorPoint(ccp(0.5f, 0.5f));
		sprCoinPad->addChild(labelCoins);

		auto btnBuy = RM->makeButton("button/blue_button_0.png", "button/blue_button_1.png", nullptr, this, menu_selector(GoldCoinScene::onBuyPack), sprCoinPad);
		btnBuy->setPosition(ccp(69, 0));
		btnBuy->setTag(100 + i);
		btnBuy->setAnchorPoint(ccp(0.5f, 1.0f));

		RM->makeButtonLabel("Buy", 20, btnBuy);
	}

	return true;
}

void GoldCoinScene::onClose(CCObject *sender) {
	CCDirector::sharedDirector()->getKeypadDispatcher()->removeDelegate(this);
	CCDirector::sharedDirector()->popScene();
}

void GoldCoinScene::keyBackClicked() {
	onClose(nullptr);
}

void GoldCoinScene::keyMenuClicked() {

}

void GoldCoinScene::onBuyPack(CCObject *sender) {
	int index = ((CCNode*)sender)->getTag() - 100;
	GDM->money += arrCoinPoints[index];
	GDM->saveMoney();

	sprintf(buf, "%d", GDM->money);
	_labelMoney->setString(buf);

	CCSprite* coinAnimation = MAKE_SPRITE("gold_icon.png");
	coinAnimation->setPosition(ccp(GM->screenSize.width / 2, GM->screenSize.height / 2));
	coinAnimation->setAnchorPoint(ccp(1.0f, 0.5f));
	_layer->addChild(coinAnimation);

	CCLabelTTF* labelCoins = CCLabelTTF::create(arrCoins[index], "", 32);
	labelCoins->setPosition(ccp(coinAnimation->getContentSize().width, coinAnimation->getContentSize().height / 2));
	labelCoins->setColor(ccc3(237, 199, 143));
	labelCoins->setAnchorPoint(ccp(0.0f, 0.5f));
	coinAnimation->addChild(labelCoins);

	coinAnimation->runAction(CCMoveBy::create(0.5f, ccp(0.0f, Y_SCALE(15.0f))));
	coinAnimation->runAction(
			CCSequence::create(
					CCDelayTime::create(0.1f),
					CCFadeOut::create(0.3f),
					CCCallFuncN::create(this, callfuncN_selector(GoldCoinScene::removeNode)),
					nullptr));
}


void GoldCoinScene::removeNode(CCNode *node) {
	node->removeFromParent();
}
